Page({
  data: {
    bgc: '#00ffff',
    percent: 0,
    bg_img: ["/images/week10/pork.png", "/images/week10/pork.png", "/images/week10/pork.png"],
    checked: false,
    light: "关灯",
    interva: "",
    imgsrc: ["/images/week10/reda.png", "/images/week10/flowera.png", "/images/week10/fanga.png"],
    disabled: false,
    game_time: 1000,
    second:10
  },
  checked(e) {
    var checked = e.detail.value
    if (checked == true) {
      this.setData({
        light: "开灯",
        bgc: "red"
      })
    } else {
      this.setData({
        light: "关灯",
        bgc: "#00ffff"
      })
    }
  },
  choice(e) {
    const bg_img = ["/images/week10/pork.png", "/images/week10/pork.png", "/images/week10/pork.png"]
    if (this.data.disabled == false) {
      wx.showToast({
        title: "请点击开始按钮",
        icon: "error"
      })
    } else {
      var i = e.currentTarget.dataset.id
      if (this.data.imgsrc[i] == "/images/week10/reda.png") {
        clearInterval(this.data.interva)
        wx.showModal({
          title: "恭喜你，猜对了！",
          showCancel: false,
          confirmText: "重玩",
          success: res => {
            this.onLoad()
            this.setData({
              percent: 0,
              disabled: false,
              bg_img: bg_img
            })
          }
        })
      } else {
        clearInterval(this.data.interva)
        wx.showModal({
          showCancel:false,
          confirmText:"重玩",
          title:"猜错了，请再来一次吧~",
          success:res=>{
            this.onLoad()
            this.setData({
              percent: 0,
              disabled: false,
              bg_img: bg_img
            })
          }
        })
      }
      this.setData({
        bg_img:this.data.imgsrc
      })
    }
  },
  radio(e) {
    var value = e.detail.value
    if (value == 0) {
      this.setData({
        game_time: 1000,
        second:10
      })
    } else {
      this.setData({
        game_time: 500,
        second:5
      })
    }
  },
  go(e) {
    wx.showToast({
      title: '游戏开始',
    })
    var that = this
    that.setData({
      disabled: true,
    })
    var times = 0
    that.data.interva = setInterval(function () {
      times += 10
      if (times > 100) {
        clearInterval(that.data.interva)
        wx.showToast({
          title: 'Game Over!',
        })
        that.setData({
          disabled: true
        })
      } else {
        that.setData({
          percent: times
        })
      }
    }, this.data.game_time)
  },
  again() {
    this.onLoad()
    clearInterval(this.data.interva)
    this.setData({
      percent: 0,
      disabled: false
    })
  },
  onLoad(options) {
    var red_index = parseInt(Math.random() * 3)
    for (var i = 0; i < 3; i++) {
      if (this.data.imgsrc[i] == "/images/week10/reda.png") {
        this.data.imgsrc[i] = this.data.imgsrc[red_index]
        this.data.imgsrc[red_index] = "/images/week10/reda.png"
      }
    }
    this.setData({
      imgsrc: this.data.imgsrc
    })
  }
})